package com.china08.yxyapi.api.home.kecheng.xiaoben;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

/**
 * 校本课程--判断用户是否vip会员/课程是否vip课程
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenUserIfVipController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolLessonAuthRepository yxyLessonsSchoolLessonAuthRepository;

    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/vip1/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        YxyLessonsSchoolLesson yxyLessonsSchoolLesson = yxyLessonsSchoolLessonRepository.findById(lessonId);

        // 图标
        YxyLessonsSchoolLessonAuth tuAuths = yxyLessonsSchoolLessonAuthRepository
                .findByUserIdAndLessonIdAndSchoolIdAndStatus(userId, lessonId, schoolId, 1);

        // List<YxyLessonsSchoolLessonAuth> list1 =
        // yxyLessonsSchoolLessonAuthRepository
        // .findByLessonIdAndSchoolId(lessonId, schoolId);

        Integer aa = yxyLessonsSchoolLesson.getVip();

        RespModel respModel = new RespModel();
        respModel.epTimes = yxyLessonsSchoolLesson.getEpTimes();
        if (tuAuths != null || aa == 0) {
            respModel.vip = true;
        } else {
            respModel.vip = false;
        }

        return respModel;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 图标：true已授权，false授权课程
        public Boolean vip;
        // 次数
        public Integer epTimes;
        // // 用户vip
        // public Boolean userVip;
        // // 课程vip
        // public Boolean kechengVip;

    }

}
